package com.atm.atmsys.Service.Imp;

import com.atm.atmsys.Mapper.AdminMapper.AdminMapper;
import com.atm.atmsys.Service.AdminService;
import com.atm.atmsys.constant.PasswordConstant;
import com.atm.atmsys.context.BaseContext;
import com.atm.atmsys.dto.AdminDTO;
import com.atm.atmsys.dto.AdminPageQueryDTO;
import com.atm.atmsys.entity.AdminInfo;
import com.atm.atmsys.entity.PageResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.List;


@Service
public class AdminServiceImp implements AdminService {

    @Autowired
    private AdminMapper adminMapper;


    //银行管理员登录
    @Override
    public AdminInfo login(AdminInfo adminInfo) {
        String password = DigestUtils.md5DigestAsHex(adminInfo.getAdminPassword().getBytes());
        adminInfo.setAdminPassword(password);
        return adminMapper.getByAdminNameAndPassword(adminInfo);
    }

    //新增银行管理员
    @Override
    public void addAdmin(AdminInfo adminInfo) {
        //设置管理员的默认密码为123456
        adminInfo.setAdminPassword(DigestUtils.md5DigestAsHex(PasswordConstant.DefaultPassword.getBytes()));

        //设置管理员的创建时间和更新时间
        adminInfo.setCreateTime(LocalDateTime.now());
        adminInfo.setUpdateTime(LocalDateTime.now());

        //设置管理员的创建人和更新人
        //TODO 这里需要根据登录用户的信息来设置，暂时先设置为0L
        adminInfo.setCreateAdmin(BaseContext.getCurrentId());
        adminInfo.setUpdateAdmin(BaseContext.getCurrentId());

        adminMapper.addAdmin(adminInfo);
    }

    //分页查询管理员信息
    @Override
    public PageResult pageQuery(AdminPageQueryDTO adminPageQueryDTO) {
        PageHelper.startPage(adminPageQueryDTO.getPage(), adminPageQueryDTO.getPageSize());
        Page<AdminInfo> page =adminMapper.pageQuery(adminPageQueryDTO);
        long total = page.getTotal();
        List<AdminInfo> result = page.getResult();
        return new PageResult(total,result);
    }

    //根据id查询管理员信息
    @Override
    public AdminInfo getAdminById(Integer id) {
        AdminInfo adminInfo=adminMapper.getAdminById(id);
        adminInfo.setAdminPassword("******");
        return adminInfo;
    }

    //修改管理员信息
    @Override
    public void updateAdmin(AdminDTO adminDTO) {
        AdminInfo adminInfo=new AdminInfo();
        BeanUtils.copyProperties(adminDTO,adminInfo);
        adminInfo.setUpdateTime(LocalDateTime.now());
        adminInfo.setUpdateAdmin(BaseContext.getCurrentId());
        adminInfo.setAdminPassword(DigestUtils.md5DigestAsHex(adminInfo.getAdminPassword().getBytes()));
        adminMapper.updateAdmin(adminInfo);

    }

    //删除管理员信息
    @Override
    public void deleteAdmin(Integer id) {
        adminMapper.deleteAdmin(id);
    }


    @Override
    public Integer getIdbyName(String name) {
        return adminMapper.getIdbyName(name);
    }
}
